/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigl.updater.parsers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 *
 * @author uxmal
 */
public class FileParser {

    Pattern use_mask, module_mask, inherit_mask, package_mask;
    File file;
    String moduleName, fileName, packageName;
    ArrayList dependencies;
    ArrayList blackList;

    public FileParser(File file) {
        this.file = file;
        inherit_mask = Pattern.compile("^use +base +qw *\\(( *[a-z0-9]+(?:\\:\\:[a-z0-9]+)*)\\) *;$", Pattern.CASE_INSENSITIVE);
        use_mask = Pattern.compile("^use +([a-z0-9]+(?:\\:\\:[a-z0-9]+)*) *(?:qw *\\(.*\\) *)? *;$", Pattern.CASE_INSENSITIVE);
        module_mask = Pattern.compile("^package ([a-z0-9]+(?:\\:\\:[a-z0-9]+)*) ?;$",Pattern.CASE_INSENSITIVE);
        package_mask = Pattern.compile("^.*/[a-z]/[a-z]{2}/[a-z]+/(?:(?:[a-z'_\\.0-9]+-)*[a-z'_\\.]+/)?(?:([a-z'_\\.0-9]+(?:-[a-z'_\\.0-9]+)*)-)?[a-z'_\\.0-9]+/.*$",Pattern.CASE_INSENSITIVE);
        
        moduleName = file.getName();
        packageName = null;
        dependencies = new ArrayList();
        fileName = file.getAbsolutePath();
        
        String[] tmp = {"strict", "warnings", "Carp", "base"};
        blackList = new ArrayList(Arrays.asList(tmp));

        parseModuleName();
        parsePackageName();
        parseDependencies();
    }

    private void parsePackageName() {

        try {
            Matcher m = package_mask.matcher(fileName);
            if ((m.matches()) && (m.groupCount() > 0)) {
                packageName = m.group(1);
            }
        } catch (PatternSyntaxException pse) {/* regex correcte */
        }
    }
    public String getPackageName() {
        return packageName;
    }
    public String getModuleName() {
        return moduleName;
    }
    private void parseModuleName() {

        BufferedReader reader = null;
        String line;

        // ouverture du fichier
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exc) {
            System.out.println("Erreur d'ouverture");
        }

        // parcours du fichier pour trouver le nom (normalement c'est la première ligne)
        try {
            while ((line = reader.readLine()) != null) {

                Matcher m = module_mask.matcher(line);
                if ((m.matches()) && (m.groupCount() > 0)) {
                    moduleName = m.group(1);
                }
            }
            reader.close();
        } catch (PatternSyntaxException pse) {/* regex correcte */
        } catch (IOException exc) {
            System.out.println("Erreur de lecture !");
        }
    }
    
    public ArrayList getDependencies() {
        return dependencies;
    }
    private void parseDependencies() {


        BufferedReader reader = null;
        String line;

        // ouverture du fichier
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exc) {
            System.out.println("Erreur d'ouverture");
        }

        // parcours du fichier pour trouver le nom (normalement c'est la première ligne)
        try {
            while ((line = reader.readLine()) != null) {
                Matcher m = use_mask.matcher(line);
                 String module = "";
                if ((m.matches()) && (m.groupCount() > 0)) {
                    module = m.group(1);
                }
                 if ((module != null) 
                         && (module.length() > 0) 
                         && !blackList.contains(module)) {
                    dependencies.add(module);
                }

            }
            reader.close();
        } catch (PatternSyntaxException pse) {/* regex correcte */
        } catch (IOException exc) {
            System.out.println("Erreur de lecture !");
        }

    }
}